﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLL;

namespace BLL
{
    public class TaiKhoanManager
    {
        public List<TAI_KHOAN> GetAll()
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from tk in db.TAI_KHOAN
                        select tk;
            return query.ToList();
        }

        public bool ThemTK(TAI_KHOAN obj)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from tk in db.TAI_KHOAN
                        where tk.ID.Equals(obj.ID)
                        select tk;
            if (!query.Any())
            {
                db.TAI_KHOAN.Add(obj);
                db.SaveChanges();
                return true;
            }
            else
                return false;
        }
        public bool CapNhatTK(TAI_KHOAN obj)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = (from tk in db.TAI_KHOAN
                         where tk.ID.Equals(obj.ID)
                         select tk).FirstOrDefault();
            if (query != null)
            {
                query.TaiKhoan = obj.TaiKhoan;
                query.MatKhau = obj.MatKhau;
                query.Quyen = obj.Quyen;
                db.SaveChanges();
                return true;
            }
            else
                return false;
        }

        public bool XoaTK(string ID)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = (from tk in db.TAI_KHOAN
                         where tk.ID == ID
                         select tk).FirstOrDefault<TAI_KHOAN>();
            if (query != null)
            {
                db.TAI_KHOAN.Remove(query);
                db.SaveChanges();
                return true;
            }
            else
                return false;
        }

        public List<TAI_KHOAN> TimMK(string TaiKhoan)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from tk in db.TAI_KHOAN
                        where tk.TaiKhoan == TaiKhoan
                        select tk;
            return query.ToList();
        }
    }
}
